# -*- coding:utf-8 -*-
"""
@author: fsksf

@since: 2022/4/9 13:47
"""
import uuid
import secrets
import re
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.websocket
import pyrestful.rest
from vcat.server.handlers import *
from vcat.server import REST_HANDLERS, OTHERS_HANDLERS
from vcat.core.engine import MainEngine
from vcat.core.constant import (
    RunningMod,
)

settings = {
    "cookie_secret": secrets.token_urlsafe(30)
}
engine = MainEngine


class Application:
    def __init__(self):
        self.engine = MainEngine

    def make_app(self, mod=RunningMod.REAL, port=9888):
        try:
            global engine
            self.engine = MainEngine(mod=mod)
            print("Start the service")
            print(REST_HANDLERS, '\n', OTHERS_HANDLERS)
            app = pyrestful.rest.RestService(REST_HANDLERS,
                                             handlers=OTHERS_HANDLERS,
                                             **settings)
            paths = [x.matcher.regex.pattern for x in app.wildcard_router.rules]
            print('\n'.join([str(h) for h in paths]))
            http_server = tornado.httpserver.HTTPServer(app)
            http_server.listen(port=port)
            tornado.ioloop.IOLoop.instance().start()
        except KeyboardInterrupt:
            print("\nStop the service")


app = Application()